import argparse

from tensorflow.python.keras.models import load_model

from gtzan import AppManager
from src.gtzan import make_dataset_dl, majority_voting

# Constants
genres = {
    'metal': 0, 'disco': 1, 'classical': 2, 'hiphop': 3, 'jazz': 4,
    'country': 5, 'pop': 6, 'blues': 7, 'reggae': 8, 'rock': 9
}

result = {}


# @RUN: Main function to call the appmanager
def main(args):
    if args.type not in ["dl", "ml"]:
        raise ValueError("Invalid type for the application. You should use dl or ml.")

    # app = AppManager(args, genres)
    # app.run()
    X = make_dataset_dl(args)
    model = load_model(args.model)

    preds = model.predict(X)
    votes = majority_voting(preds, genres)
    print("{} is a {} song".format(args.song, votes[0][0]))
    print("most likely genres are: {}".format(votes[:3]))
    result[args.song] = votes[0][0]


if __name__ == '__main__':
    # Parse command line arguments
    parser = argparse.ArgumentParser(description='Music Genre Recognition on GTZAN')

    # Required arguments
    parser.add_argument('-t', '--type', help='dl or ml for Deep Learning or Classical ML approaches, respectively.',
                        type=str, required=True)

    # Nearly optional arguments. Should be filled according to the option of the requireds
    parser.add_argument('-m', '--model', help='Path to trained model', type=str, required=True)
    parser.add_argument('-s', '--song', help='Path to song to classify', type=str, required=True)
    args = parser.parse_args()

    with open("D:/CloudMusic/index.txt", 'r') as file:
        songs = [song.strip() for song in file]
    for song in songs:
        args.song = song
        # Call the main function
        main(args)
    for k,v in result.items():
        print(f"{k},{v}")

